$(function(){
	$(".sysMesage").hover(function(){
		$(this).children("div").show();
	},
	function(){
		$(this).children("div").hide();
	});
	
	$(".tableDrop").parent("th").click(function(){
		$(this).parents(".noBorderChild").find("tr").not(":first").slideToggle();
		$(this).children(".tableDrop").toggleClass("tableDropShow"); 
	});
	
	$(".noChosePp").click(function(){
		$(this).toggleClass("chosePp");
	});
	
	$(".nochoose").click(function(){
		$(this).toggleClass("choose");
	});
	$("#b_read").click(function(){
		$(this).prev().toggleClass("choose");
	})
	$(".nochoose.all,#b_read").click(function(){ 
		if($("td label.choose").size()==0){
			layer.alert('您未选择任何内容');
			return;
		}
		if(1==1){
			var chooseids="";
			var noReadCount=0;
			$(".choose").each(function(){
				var msgdata=$(this).siblings("dfn").attr("id");
				if($(this).parents("tr").not(".noReaded.readed").hasClass("noReaded")){
					noReadCount=noReadCount+1;
				}
				if(typeof(msgdata)!="undefined"){
					var msgid=msgdata.substring(3,msgdata.length);
					if(chooseids==""){
						chooseids+=msgid;
					}else{
						chooseids+=","+msgid;
					}
				}
			})
			if(noReadCount>0){//批量修改已读状态
				 var layerIndex; 
				 $.ajax({
			          url: 'changeAlltoRead',
			          data: {chooseids:chooseids},
			          type: 'post',
			          dataType : 'json',
					  beforeSend : function () {
						  layerIndex = layer.load(1, {
							  shade: [0.1,'#fff'] //0.1透明度的白色背景
							});
			          },
			          success: function (data) {
			             	 if(data.success){
			             		$(".choose").parents("tr").addClass("readed");
			             		
			             		$("#noReadCount").text(parseInt($("#noReadCount").text())-noReadCount);
		            			$("#msgHeadCount").text(parseInt($("#msgHeadCount").text())-noReadCount);
			             	 }
			          },
					  complete : function() {
							layer.close(layerIndex);
					  },
					  error:function(xx){
							console.log(xx)
					  }
			      });
			}
		} 
	});
	/*未读——已读**/
	$("tr dfn").click(function(){
		var msgdata=$(this).attr("id");
		var msgid=msgdata.substring(3,msgdata.length);
		var node=$(this);
		if(node.parents("tr").hasClass("readed")){
			node.parents("tr").siblings("tr").next(".xunJiaDetail").hide();
     		node.parents("tr").addClass("readed");
     		node.parents("tr").next(".xunJiaDetail").slideToggle();
     		return;
		}
		if(msgid!=""){
			 var layerIndex;
			 $.ajax({
		          url: 'changeMsgReaded',
		          data: {id:msgid},
		          type: 'post',
		          dataType : 'json',
				  beforeSend : function () {
					  layerIndex = layer.load(1, {
						  shade: [0.1,'#fff'] //0.1透明度的白色背景
						});
		          },
		          success: function (data) {
		             	if(data.success){
		             		node.parents("tr").siblings("tr").next(".xunJiaDetail").hide();
		             		node.parents("tr").addClass("readed");
		             		node.parents("tr").next(".xunJiaDetail").slideToggle();
		             		
		             		$("#noReadCount").text(parseInt($("#noReadCount").text())-1);
	            			$("#msgHeadCount").text(parseInt($("#msgHeadCount").text())-1);
		             	}else{
		             		layer.alert("出错了");
		             	}
		          },
				  complete : function() {
						layer.close(layerIndex);
				  },
				  error:function(xx){
						console.log(xx)
				  }
		      });
		}
	});
})
//取消和勾选发送(普通消息)
function doCancle(code,node){
	 var layerIndex;
	 $.ajax({
         url: 'clickCheckBox',
         data: {code:code},
         type: 'post',
         dataType : 'json',
		 beforeSend : function () {
			 layerIndex = layer.load(1, {
				  shade: [0.1,'#fff'] //0.1透明度的白色背景
				});
         },
         success: function (data) {
            	 if(data.success){
            		 layer.msg('操作成功', {
            			  icon: 1,
            			  time: 1500 
            			});   
            	 }else{
            		 alert(data.msg);
            	 }
         },
		  complete : function() {
				layer.close(layerIndex);
		  },
		  error:function(xx){
				console.log(xx)
		  }
     });
}
//询价消息勾选和取消
function doEnqClick(type,eid,cid){
	 var layerIndex;
	 $.ajax({
         url: 'clickEnqCheckBox',
         data: {type:type,eid:eid,cid:cid},
         type: 'post',
         dataType : 'json',
		 beforeSend : function () {
			 layerIndex = layer.load(1, {
				  shade: [0.1,'#fff'] //0.1透明度的白色背景
				});
         },
         success: function (data) {
            	 if(data.success){
            		 layer.msg('操作成功', {
            			  icon: 1,
            			  time: 1500 
            			});   
            	 }else{
            		 alert(data.msg);
            	 }
         },
		 complete : function() {
				layer.close(layerIndex);
		 },
		 error:function(xx){
				console.log(xx)
		 }
     });
}
/*删除所选**/
function delChose(o) {
	var chooseids="";
	var noReadCount=0;
	$(".choose").each(function(){
		var msgdata=$(this).siblings("dfn").attr("id");
		if($(this).parents("tr").not(".noReaded.readed").hasClass("noReaded")){
			noReadCount=noReadCount+1;
		}
		if(typeof(msgdata)!="undefined"){
			var msgid=msgdata.substring(3,msgdata.length);
			if(chooseids==""){
				chooseids+=msgid;
			}else{
				chooseids+=","+msgid;
			}
		}
	});
	if(chooseids!="") {//批量删除
		layer.open({
			content: '确认删除选中的消息吗？',
			yes: function(index, layero) {
				var layerIndex;
				$.ajax({
					url: 'delchooseMsg',
					data: {chooseids:chooseids},
					type: 'post',
					dataType : 'json',
					beforeSend : function () {
						layerIndex = layer.load(1, {
							shade: [0.1,'#fff'] //0.1透明度的白色背景
						});
					},
					success: function (data) {
						if(data.success){
							$("label[class='nochoose "+o+"']").parents(".readed").next(".xunJiaDetail").remove();
							$("label[class='nochoose "+o+"']").parents(".readed").remove();
							$("label[class='nochoose "+o+"']").parents(".noReaded").next(".xunJiaDetail").remove();
							$("label[class='nochoose "+o+"']").parents(".noReaded").remove();
							var count=parseInt($("#noReadCount").text())-noReadCount;
							if(count>0){
								$("#noReadCount").text(parseInt($("#noReadCount").text())-noReadCount);
								$("#msgHeadCount").text(parseInt($("#msgHeadCount").text())-noReadCount);
							}else{
								location.reload();
							}
							layer.close(index);
						}
					},
					complete : function() {
						layer.close(layerIndex);
					},
					error:function(xx){
						console.log(xx)
					}
				});
			}
		})
	} else {
		layer.alert("未选中任何消息");
	}

}
/*删除所有**/
function delAll(){ 
	layer.open({
		  content: '确认删除本页的所有消息吗？',
		  yes: function(index, layero){
			  var layerIndex;
			  var chooseids="";
				$("dfn").each(function(){
					var msgdata=$(this).attr("id");
					if(typeof(msgdata)!="undefined"){
						var msgid=msgdata.substring(3,msgdata.length);
						if(chooseids==""){
							chooseids+=msgid;
						}else{
							chooseids+=","+msgid;
						}
					}
				})
				if(chooseids!=""){//删除本页
					 var layerIndex;
					 $.ajax({
				          url: 'delchooseMsg',
				          data: {chooseids:chooseids},
				          type: 'post',
				          dataType : 'json',
						  beforeSend : function () {
							  layerIndex = layer.load(1, {
								  shade: [0.1,'#fff'] //0.1透明度的白色背景
								});
				          },
				          success: function (data) {
				             	 if(data.success){
				             		$("#allresource").children().remove(); 
				            		layer.close(index); 
				            		location.reload();
				             	 }
				          },
						  complete : function() {
								layer.close(layerIndex);
						  },
						  error:function(xx){
								console.log(xx)
						  }
				      });
				}else{
					layer.close(index);
					layer.alert("没有任何消息");
				}
				
		  }
	});        
}